package com.notice.rule.limit;

import com.hqjl.redis.redisson.RedissonClientBuilder;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;

/**
 * 分布式限流
 *
 * @author DZL
 * @date 2023/1/31 14:15
 */
public class DistributedLimit implements IRateLimit {
    public RRateLimiter rateLimiter;

    public DistributedLimit(String rateName) {
        RedissonClient redissonClient = RedissonClientBuilder.getRedissonClient();
        rateLimiter = redissonClient.getRateLimiter(rateName);
        rateLimiter.trySetRate(RateType.PER_CLIENT, 19, 60, RateIntervalUnit.SECONDS);
    }

    @Override
    public void acquire() {
        rateLimiter.acquire();
    }
}
